<?php
function CopyFiles($dir) {
  foreach($_REQUEST['files'] as $file) {
    $src = $_SESSION['dir'].$file;
    $dst = $_SESSION['dir'].$dir.strrchr($file,"/");
    rcopy($src, $dst);
  }
  echo "Success: [".count($_REQUEST['files'])."] Files copied successfully!";
}

function rcopy($src, $dst) {
  if(file_exists($dst)) { echo "File [$dst] already exists!"; return false; }
  if(is_dir($src)) {
    mkdir($dst);
    $files = scandir($src);
    foreach ($files as $file)
    if($file != "." && $file != "..") rcopy("$src/$file", "$dst/$file");
  }	else if(file_exists($src)) copy($src, $dst);
}

require_once('../../conf/main.conf.php');
if(isset($_REQUEST['files']) && !empty($_REQUEST['files']) && isset($_REQUEST['destdir']) && !empty($_REQUEST['destdir'])) {
  CopyFiles($_REQUEST['destdir']);
}
else die("Unauthorized access");

?>
